package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//此类适用来测试jdbc连接数据库并做插入数据操作演示的demo
public class TestInert {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Drvier");
        String ulr = "jdbc:mysql://localhost:3306/tedu?useUnicode=true&characterEcoding=utf8&serverTimezone=Asia/Shanghai";
        String user = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection("url", "user", "passwodr");
        System.out.println("连接成功...");
        //在location表中插入一条记录，id为Null,name为"神之国度"
        //INSERT INTO location(id,name) VALUES(NULL ,'神之国度’)；
        //1.定义一条预编译的sql语句，交给sql变量保存
        String sql = "INSERT INTO location(id,name) VALUES(NULL,'神之国度')";
        //2创建一个PrepareStatement对象，用conn调用prepareStatement方法
        PreparedStatement ps = conn.prepareStatement(sql);
        //3进行对应功能的调用
        int rows = ps.executeUpdate();

        System.out.println(rows > 0 ? "插入成功" : "插入失败");
        ps.close();
        conn.close();
    }


}
